package com.gp.controller.goods;


import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.controller.BaseController;
import com.gp.model.dto.Appraises;
import com.gp.model.dto.GoodsDetailDto;
import com.gp.model.entity.BaseGoods;
import com.gp.service.IBaseGoodsPropertyService;
import com.gp.service.IBaseGoodsService;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@RestController
@Api(value="goods",produces=MediaType.APPLICATION_JSON_VALUE)
@RequestMapping("/goods")
public class GoodsController extends BaseController {
  @Autowired
  private IBaseGoodsService goodsService;
  @Autowired
  private IBaseGoodsPropertyService goodsPropertyService;
  
  /**
   * 分页搜索获取商城列表
   * @param page
   * @return
   */
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public JsonResult listGoods(Page<BaseGoods> page) {
    Page<BaseGoods> pages = goodsService.listGoods(page);
    return renderSuccess("查询成功！",pages);
  }
  
  /**
   * 更新商品信息
   * @param page
   * @return
   */
  @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
  public JsonResult update(BaseGoods entity) {
    return goodsService.updateById(entity) ? renderSuccess("更新成功！") :renderError("更新失败！");
  }
  
  /**
   * 添加一个商品，必须添加商品对应的私有属性 
   * @param goods 需要增加的商品信息
   * @param bindingResult
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public JsonResult addNewGoods(@Valid GoodsDetailDto goods, BindingResult bindingResult) {
    boolean flag = goodsService.insertGoods(goods);
    return flag ? renderSuccess("添加成功！") : renderError("商品添加失败！");
  }
  
  /**
   * 获取商品的详情
   * @param goodsId 通过id获取商品的详情
   * @return
   */
  @RequestMapping(value = "/detail/{goodsId}", method = RequestMethod.GET)
  public JsonResult obtainGoodDetail(@NotNull @PathVariable String goodsId) {
    GoodsDetailDto good = goodsService.selectGoodsById(Long.parseLong(goodsId));
    return good != null ? renderSuccess("查询成功！",good) : renderSuccess("未找到数据！");
  }
 
  /**
   * 删除一个商品
   * @param id 需要删除的商品的id
   * @return
   */
  @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
  public JsonResult deleteGoods(@PathVariable String id) {
    boolean flag = goodsService.deleteById(id);
    return flag ? renderSuccess("删除成功！") : renderError("该商品删除失败！");
  }
  
  @ApiOperation(value = "获取商品属性信息",httpMethod = "GET",response=JsonResult.class,produces=MediaType.APPLICATION_JSON_VALUE,notes="获取商品的所有的属性信息，可用于首页的商品搜索")
  @RequestMapping(value = "/listGoodsProperties", method = RequestMethod.GET)
  public JsonResult listGoodsProperties() {
    Map<String, List<String>> map = goodsPropertyService.listGoodsProperties();
    return renderSuccess("查询成功！",map);
  }
  
  /**
   * 获取商品评论 
   * @param page
   * @return
   */
  @RequestMapping(value = "/listGoodsAppraises", method = RequestMethod.GET)
  public JsonResult listGoodsAppraises(Page page) {
    Page<Appraises> map = goodsService.listGoodsAppraises(page,page.getCondition());
    return renderSuccess("查询成功！",map);
  }
}


